"""
This module contains the class for the character Roy. Modifications to
his object should be done here.
"""

import pygame, math
import items
from pygame.locals import *
from qnd import *

SPEED = 5
inventory_back = load_image('inventory.png', alpha=False)
inventory_font = pygame.font.Font(None,16)

class Roy:
	"""
	This is the main class for the character Roy. Its update method
	should be called every frame as well as its draw method.
	"""
	sprites = [load_image('roy#3sidevieweast.png'),load_image('roy#3frontview.png'),load_image('roy#3sideviewwest.png'),load_image('roy#3backview.png')]
	
	def __init__(self, (x,y)):
		self.x,self.y = x,y #position in pixels
		self.sprite = self.sprites[1]
		self.speed = 5 #The maximum speed Roy runs at
		self.direction = 0 #The direction Roy is facing
		self.inventory = [] #Is a list of items Roy is holding
		self.weapon = None #The current weapon Roy has
	def update(self,input,tilemap,collision_map):
		"""Call this every frame"""
		self.direction = math.degrees(math.atan2(self.y-input.mousey,input.mousex-self.x)) #Make roy face towards the mouse. You can ignore the trigenometry if you want.
		#Move based on which keys are pressed
		if input.keys[K_w]:
			if self.get_free((self.x,self.y-self.speed),collision_map):
				self.y -= self.speed
			self.sprite = self.sprites[3]
		if input.keys[K_s]:
			if self.get_free((self.x,self.y+self.speed),collision_map):
				self.y += self.speed
			self.sprite = self.sprites[1]
		if input.keys[K_a]:
			if self.get_free((self.x-self.speed,self.y),collision_map):
				self.x -= self.speed
			self.sprite = self.sprites[2]
		if input.keys[K_d]:
			if self.get_free((self.x+self.speed,self.y),collision_map):
				self.x += self.speed
			self.sprite = self.sprites[0]
	def get_free(self,(x,y),collision_map):
		"""Returns True if Roys would not collide at position x,y"""
		x1,y1 = get_tile_position((x-8,y-8))
		try:
			if collision_map[x1][y1]:
				return False
			x2,y2 = get_tile_position((x+8,y-8))
			if collision_map[x2][y2]:
				return False
			x3,y3 = get_tile_position((x-8,y+16))
			if collision_map[x3][y3]:
				return False
			x4,y4 = get_tile_position((x+8,y+16))
			if collision_map[x4][y4]:
				return False
			return True
		except IndexError:
			return True
			
		return collision_map[collision_x][collision_y]
	def draw(self,screen,camera):
		"""Call this function to draw roy onto the screen"""
		draw_image(screen, self.sprite, (self.x,self.y), center=True,camera=camera) #Draw the roy sprite rotated in the direction he's facing
	def draw_inventory(self,screen):
		"""Draw roy's inventory on the screen"""
		draw_image(screen, inventory_back, (0,0), alpha=235)
		columns = 5        #number of columns
		icon_width = 64    #width of each icon
		icon_height = 64   #height of each icon
		icon_spacing = 32  #distance in between icons
		
		start_x = 32
		start_y = 160
		
		for i,item in enumerate(self.inventory):
			x = (i%columns)*(icon_width+icon_spacing) + start_x
			y = (i/columns)*(icon_height+icon_spacing) + start_y
			draw_image(screen, item.icon, (x,y))
			draw_text(screen,item.name,(x,y+icon_height),inventory_font)
			
	def give_item(self,item):
		"""Give Roy an item."""
		self.inventory.append(item)
	def get_tile_position(self, tilewidth=32,tileheight=32):
		"""Return the tile Roy's center is on"""
		return int(self.x)/tilewidth,int(self.y)/tileheight
